package com.kili.picture.tools;

import com.kili.picture.context.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.async.WebAsyncTask;

import java.util.concurrent.Callable;

import static com.kili.picture.common.AsyncTaskConst.TASK_TIME_OUT;

/**
 * @author Gordon
 * @version 2019/5/15
 */
@Component
public class WebAsyncTaskHelper {

    @Autowired
    @Qualifier("taskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;

    public WebAsyncTask<Result> asyncCall(Callable<Result> task,
                                          Callable<Result> error,
                                          Callable<Result> timeout) {
        WebAsyncTask<Result> asyncTask = new WebAsyncTask<>(TASK_TIME_OUT, taskExecutor, task);
        asyncTask.onError(error);
        asyncTask.onTimeout(timeout);
        return asyncTask;
    }
}
